package cnic.ai.nacos.config

import org.quartz.*
import org.quartz.SimpleScheduleBuilder.simpleSchedule
import org.quartz.impl.StdSchedulerFactory
import org.springframework.context.annotation.Configuration
import org.springframework.stereotype.Component
import javax.annotation.PostConstruct


@Configuration
open class QuartzScheduler {

    //@Autowired
    //private var providerClient: NacosDiscoveryClient2?=null

    lateinit var scheduler: org.quartz.Scheduler
    lateinit var trigger: Trigger

    @PostConstruct
    fun schedulerFunc() {
        // 1.创建调度器 Scheduler
        val sf: SchedulerFactory = StdSchedulerFactory()
        scheduler = sf.scheduler

        var jobDataMap = JobDataMap()
        //jobDataMap["client"] = providerClient

        // 2.创建JobDetail实例，并与QuertzJob类绑定(Job执行内容)
        val job = JobBuilder.newJob(QuartzJob::class.java)
            .withIdentity("job1", "group1")
            .setJobData(jobDataMap)
            .build()

        // 3.构建Trigger实例,每隔30s执行一次
        trigger = TriggerBuilder.newTrigger()
            .withIdentity("trigger1", "group1")
            .startNow()
            .withSchedule(
                simpleSchedule()
                    .withIntervalInSeconds(10)
                    .repeatForever()
            )
            .build()

        // 4.执行，开启调度器
        scheduler?.scheduleJob(job, trigger)
        println("=====================QuertzScheduler=========================================")
        println(System.currentTimeMillis())
        scheduler?.start()
    }
}